package com.darren.befriend.fragments;

import java.util.ArrayList;

import com.darren.befriend.R;
import com.darren.befriend.adapters.UniversityAdapter;
import com.darren.befriend.utilities.Utilities;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnCloseListener;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;

public class UniversityFragment extends Fragment implements
		OnItemClickListener, OnQueryTextListener, OnCloseListener,
		OnClickListener {

	private static String UNIVERSITY = "university";
	private ArrayList<String> mUniversityArr;
	private UniversityAdapter mAdapter;
	private SearchView mUniversitySearch;

	OnUniversitySelected mUserniversityCallback;

	// Container Activity must implement this interface
	public interface OnUniversitySelected {
		public void onUserniverSelected(int selectedUniver);
	}

	public static Fragment create() {
		UserSettingFragment fragment = new UserSettingFragment();
		return fragment;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onAttach(android.app.Activity)
	 */
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		// This makes sure that the container activity has implemented
		// the callback interface. If not, it throws an exception
		try {
			mUserniversityCallback = (OnUniversitySelected) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnHeadlineSelectedListener");
		}
	}

	public static Fragment create(ArrayList<String> university) {
		UniversityFragment fragment = new UniversityFragment();
		Bundle bundle = new Bundle();
		bundle.putStringArrayList(UNIVERSITY, university);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (savedInstanceState == null) {
			if (getArguments() != null) {

				mUniversityArr = getArguments().getStringArrayList(UNIVERSITY);
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mainView = (ViewGroup) inflater.inflate(
				R.layout.layout_university_fragment, container, false);
		mUniversitySearch = (SearchView) mainView
				.findViewById(R.id.uniservity_search);
		mUniversitySearch.onActionViewExpanded();
		mUniversitySearch.setIconified(false);
		mUniversitySearch.setOnQueryTextListener(this);
		mUniversitySearch.setOnCloseListener(this);
		mUniversitySearch.clearFocus();
		TextView cancelBtn = (TextView) mainView
				.findViewById(R.id.uniservity_search_cancel);
		cancelBtn.setOnClickListener(this);
		ListView universityLst = (ListView) mainView
				.findViewById(R.id.university_list);
		ArrayList<String> tempArr = (ArrayList<String>) mUniversityArr.clone();
		mAdapter = new UniversityAdapter(getActivity(), tempArr);
		universityLst.setAdapter(mAdapter);
		universityLst.setOnItemClickListener(this);
		return mainView;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
	 * .AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> adapterView, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub

		if (adapterView.getAdapter().getCount() == mUniversityArr.size()) {

			mUserniversityCallback.onUserniverSelected(arg2);
		} else {

			String selectedUni = (String) adapterView.getAdapter()
					.getItem(arg2);
			for (int i = 0; i < mUniversityArr.size(); i++) {

				String univer = mUniversityArr.get(i);
				if (univer.equals(selectedUni)) {

					mUserniversityCallback.onUserniverSelected(i);
					break;
				}
			}
		}

		Utilities.hideKeyboard(getActivity());
		getActivity().getSupportFragmentManager().popBackStack();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.SearchView.OnQueryTextListener#onQueryTextChange(java.
	 * lang.String)
	 */
	@Override
	public boolean onQueryTextChange(String query) {
		// TODO Auto-generated method stub

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.SearchView.OnQueryTextListener#onQueryTextSubmit(java.
	 * lang.String)
	 */
	@Override
	public boolean onQueryTextSubmit(String query) {
		// TODO Auto-generated method stub

		if (mUniversityArr != null) {

			ArrayList<String> tempArr = new ArrayList<String>();
			for (String univer : mUniversityArr) {

				if (univer.toLowerCase().contains(query.toLowerCase())) {

					tempArr.add(univer);
				}
			}

			mAdapter.setDatas(tempArr);
			mAdapter.notifyDataSetChanged();
		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.SearchView.OnCloseListener#onClose()
	 */
	@Override
	public boolean onClose() {
		// TODO Auto-generated method stub

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {

		case R.id.uniservity_search_cancel:

			mUniversitySearch.setQuery("", false);
			mUniversitySearch.clearFocus();
			Utilities.hideKeyboard(getActivity());
			ArrayList<String> tempArr = (ArrayList<String>) mUniversityArr
					.clone();
			mAdapter.setDatas(tempArr);
			mAdapter.notifyDataSetChanged();
			break;
		}
	}
}
